<template>
  <dialog-editor ref="dialog"></dialog-editor>
</template>

<script>
import DialogEditor from '@/components/DialogEditor'
import dialogMixins from '@/utils/commonDialogMixins'
import {mRequest} from '@/utils/request'

export default {
  mixins: [dialogMixins],
  components: {DialogEditor},
  data() {
    return {
      orderDetailList: [],
      productInfoList: [],
      clientInfoList: [],
      warehouseInfoList: [],
      dataFormConfig: {
        id: {
          hidden: true
        },
        orderDetailId: {
          label: '订单信息',
          type: 'list',
          list: [],
          filterable: true,
          placeholder: '订单信息',
          selectCallback: this.orderDetailIdChanged
        },
        productId: {
          label: '产品',
          type: 'list',
          list: [],
          filterable: true,
          placeholder: '请选择产品'
        },
        clientId: {
          label: '客户名称',
          type: 'list',
          list: [],
          filterable: true,
          placeholder: '请选择客户'
        },
        salespersonId: {
          label: '销售',
          type: 'list',
          list: [],
          filterable: true,
          placeholder: '请选择销售'
        },
        productWeight: {
          label: '克数',
          type: 'number',
          max: 1000,
          default: 0,
          selectCallback: this.productWeightChanged
        },
        boxNumber: {
          label: '箱数',
          type: 'number',
          max: 100000,
          default: 0,
          selectCallback: this.numberChanged
        },
        boxZhiNumber: {
          label: '只数',
          type: 'number',
          max: 1000,
          default: 0,
          selectCallback: this.numberChanged
        },
        assortBoxNumber: {
          label: '配箱数量',
          type: 'number',
          max: 10000,
          default: 0,
          selectCallback: this.numberChanged
        },
        assortBoxZhiNumber: {
          label: '配箱只数',
          type: 'number',
          max: 1000,
          default: 0,
          selectCallback: this.numberChanged
        },
        tailBoxNumber: {
          label: '尾箱数量',
          type: 'number',
          max: 1000,
          default: 0,
          selectCallback: this.numberChanged
        },
        tailBoxZhiNumber: {
          label: '尾箱只数',
          type: 'number',
          max: 1000,
          default: 0,
          selectCallback: this.numberChanged
        },
        scrapNumber: {
          label: '零头',
          type: 'number',
          max: 100000,
          default: 0,
          selectCallback: this.numberChanged
        },
        countProductNumber: {
          label: '总数量',
          type: 'number',
          max: 1000000,
          default: 0,
          selectCallback: this.numberChanged
        },
        boxNumberCount: {
          label: '总箱数',
          type: 'number',
          max: 100000000
        },
        weightCount: {
          label: '总克数',
          type: 'number',
          max: 100000000
        },
        putInTime: {
          label: '入库时间',
          type: 'date',
          showInMultiMode: true,
          placeholder: '请选择入库时间'
        },
        location: {
          label: '存放位置',
          placeholder: '请输入存放位置'
        },
        // trayNumber:{
        //   label: '托数',
        //   type: 'number',
        //   max: 10000,
        //   default: 0,
        // },
        remark: {
          label: '备注',
          inputType: 'textarea',
          placeholder: '请输入备注'
        },
        status: {
          label: '状态',
          type: 'switch',
          activeValue: 1,
          inactiveValue: 0,
          default: 1
        }
      },
      editTableConfig: {
        orderDetailId: {
          title: '订单编号',
          type: 'select',
          valueChangeEvent: this.changeOrderByOrderDetailId
        },
        productNo: {
          title: '产品名称',
        },
        clientName: {
          title: '客户',
        },
        productWeight: {
          title: '克重',
          type: 'number',
          min: 0
        },
        boxZhiNumber: {
          title: '只数',
          type: 'number',
          default: 0,
          min: 0,
          valueChangeEvent: this.multipleNumberChanged
        },
        boxNumber: {
          title: '箱数',
          type: 'number',
          default: 0,
          min: 0,
          valueChangeEvent: this.multipleNumberChanged
        },
        assortBoxNumber: {
          title: '配箱数量',
          type: 'number',
          min: 0,
          default: 0,
          valueChangeEvent: this.multipleNumberChanged
        },
        assortBoxZhiNumber: {
          title: '配箱只数',
          type: 'number',
          min: 0,
          default: 0,
          valueChangeEvent: this.multipleNumberChanged
        },
        tailBoxNumber: {
          title: '尾箱数量',
          min: 0,
          type: 'number',
          default: 0,
          valueChangeEvent: this.multipleNumberChanged
        },
        tailBoxZhiNumber: {
          title: '尾箱只数',
          type: 'number',
          max: 1000,
          default: 0,
          valueChangeEvent: this.multipleNumberChanged
        },

        scrapNumber: {
          title: '零头',
          type: 'number',
          default: 0,
          min: 0,
          valueChangeEvent: this.weightChangedHandle
        },
        boxNumberCount: {
          title: '总箱数',
          type: 'number',
          max: 1000000
        },
        countProductNumber: {
          title: '总只数',
          type: 'number',
          default: 0,
          min: 0
        },
        // trayNumber:{
        //   title: '托数',
        //   type: 'number',
        //   min: 0,
        //   default: 0,
        // },
        location: {
          title: '存放位置',
          type: 'select',
          optionsGet: () => {
            return this.warehouseInfoList
          }
        },
        remark: {
          title: '备注',
          inputType: 'textarea',
          placeholder: '请输入备注'
        }
      }
    }
  },
  methods: {
    changeOrderByOrderDetailId(row) {
      const {orderDetailId} = row

      if (this.R.isNil(orderDetailId) || this.R.equals('', orderDetailId)) {
        row.orderDetailId = ''
        row.productId = ''
        row.productNo = ''
        row.clientId = ''
        row.clientName = ''
        row.assortBoxNumber = 0
        row.assortBoxZhiNumber = 0
        row.tailBoxNumber = 0
        row.tailBoxZhiNumber = 0
        row.productWeight = 0
        row.boxSupplyWay = 0
        row.trayLayerNumber = 0
        row.trayEachZhiNumber = 0
        row.boxNumber = 0
        row.boxZhiNumber = 0
        row.scrapNumber = 0
        row.countProductNumber = 0
        row.weightCount = 0
        return
      }
      mRequest('orderApi/orderDetailInfo', {id: orderDetailId}, (data) => {
        const orderDetail = this.R.pathOr({}, ['data', 'orderDetail'], data)

        if (this.R.isNil(orderDetail)) {
          return
        }
        row.productId = orderDetail.productId
        row.productNo = orderDetail.productNo
        row.clientId = orderDetail.clientId
        row.clientName = orderDetail.clientName
        row.productWeight = orderDetail.productWeight
        row.boxSupplyWay = orderDetail.boxSupplyWay
      })
    },
    productChanged() {
      const productId = this.R.propOr(0, 'productId', this.dataForm)

      if (this.R.isNil(productId) || this.R.isEmpty(productId)) {
        return
      }
      mRequest('productApi/productInfoInfo', {id: productId}, ({data}) => {
        this.dataForm.productWeight = this.R.pathOr(0, ['productInfo', 'productWeight'], data)
        this.dataForm.boxId = this.R.pathOr(null, ['productInfo', 'boxId'], data)
        this.dataForm.trayInfoId = this.R.pathOr(null, ['productInfo', 'boxId'], data)
      })
    },
    orderDetailIdChanged() {
      const orderDetailId = this.R.propOr(0, 'orderDetailId', this.dataForm)

      if (this.R.isNil(orderDetailId) || this.R.isEmpty(orderDetailId)) {
        return
      }

      mRequest('orderApi/orderDetailInfo', {id: orderDetailId}, ({data}) => {
        this.dataForm.productId = this.R.pathOr(0, ['orderDetail', 'productId'], data)
        this.dataForm.productWeight = this.R.pathOr(0, ['orderDetail', 'productWeight'], data)
        this.dataForm.clientId = this.R.pathOr(0, ['orderDetail', 'clientId'], data)
        this.dataForm.salespersonId = this.R.pathOr(0, ['orderDetail', 'createUser'], data)
      })
    },

    numberChanged() {
      const productWeight = this.R.propOr(0, 'productWeight', this.dataForm)

      const boxNumber = this.R.propOr(0, 'boxNumber', this.dataForm)
      const boxZhiNumber = this.R.propOr(0, 'boxZhiNumber', this.dataForm)
      const scrapNumber = this.R.propOr(0, 'scrapNumber', this.dataForm)
      const assortBoxNumber = this.R.propOr(0, 'assortBoxNumber', this.dataForm)
      const assortBoxZhiNumber = this.R.propOr(0, 'assortBoxZhiNumber', this.dataForm)
      const tailBoxNumber = this.R.propOr(0, 'tailBoxNumber', this.dataForm)
      const tailBoxZhiNumber = this.R.propOr(0, 'tailBoxZhiNumber', this.dataForm)

      this.dataForm.countProductNumber = (boxNumber * boxZhiNumber) + (assortBoxNumber * assortBoxZhiNumber) + (tailBoxNumber * tailBoxZhiNumber) + scrapNumber

      this.dataForm.weightCount = this.dataForm.countProductNumber * productWeight
      this.dataForm.boxNumberCount = boxNumber + assortBoxNumber + tailBoxNumber
    },
    multipleNumberChanged(row) {
      console.log(row)
      const productWeight = this.R.propOr(0, 'productWeight', row)
      const boxNumber = this.R.propOr(0, 'boxNumber', row)
      const boxZhiNumber = this.R.propOr(0, 'boxZhiNumber', row)
      const scrapNumber = this.R.propOr(0, 'scrapNumber', row)
      const assortBoxNumber = this.R.propOr(0, 'assortBoxNumber', row)
      const assortBoxZhiNumber = this.R.propOr(0, 'assortBoxZhiNumber', row)
      const tailBoxNumber = this.R.propOr(0, 'tailBoxNumber', row)
      const tailBoxZhiNumber = this.R.propOr(0, 'tailBoxZhiNumber', row)

      row.countProductNumber = boxNumber * boxZhiNumber + assortBoxNumber * assortBoxZhiNumber + tailBoxNumber * tailBoxZhiNumber + scrapNumber

      row.weightCount = row.countProductNumber * productWeight
      row.boxNumberCount = (Number(boxNumber)) + (Number(assortBoxNumber)) + (Number(tailBoxNumber))
    },

    weightChangedHandle(row) {
      const productWeight = this.R.propOr(0, 'productWeight', row)

      const boxNumber = this.R.propOr(0, 'boxNumber', row)
      const boxZhiNumber = this.R.propOr(0, 'boxZhiNumber', row)
      const scrapNumber = this.R.propOr(0, 'scrapNumber', row)
      row.countProductNumber = boxNumber * boxZhiNumber + scrapNumber

      row.weightCount = row.countProductNumber * productWeight
    },
    productWeightChanged() {
      this.numberChanged()
    },
    addNewProduct() {
      const cb = (newValue) => {
        this.initProductInfoList()
        this.dataForm.productId = newValue.id
      }
      this.$parent.openProductInfoDialog(cb)
    },
    addNewClientInfo() {
      const cb = (newValue) => {
        this.initClientInfoList()
        this.dataForm.clientId = newValue.id
      }
      this.$parent.openClientInfoDialog(cb)

    },
    // orderDetailFilterList(query = '') {
    //   const arr = this.orderDetailList.filter(item => {
    //     return item.name.toLowerCase().includes(query.toLowerCase())
    //   })
    //
    //   if (arr.length > 100) {
    //     this.orderDetailListFilter = arr.slice(0, 100)
    //   } else {
    //     this.orderDetailListFilter = arr
    //   }
    // },
    initOrderDetailList() {
      mRequest('orderApi/getOrderDetailList', {}, (res) => {
        this.dataFormConfig['orderDetailId'].list = this.R.pathOr([], ['data', 'list'], res)
      })
    },
    async init(id, multiAdd) {
      this.dataForm.id = id || 0
      this.initProductInfoList()
      // this.initBoxInfoIdList('boxId')
      // this.initTrayList()
      this.initClientInfoList()
      this.initSysUserInfoList('salespersonId')
      const props = {
        dataForm: this.dataForm,
        config: this.dataFormConfig,
        dataRule: this.getNeedDataRule( 'clientId', 'productWeight', 'trayInfoId', 'zhiNumber', 'putInTime', 'status'),
        initApi: 'productApi/productPutInStorageInfo',
        saveApi: 'productApi/saveProductPutInStorage',
        updateApi: 'productApi/updateProductPutInStorage',
        objectName: 'productPutInStorage'
      }

      if (multiAdd) {
        mRequest('warehouseApi/getWarehouseInfoList', {}, res => {
          this.warehouseInfoList = this.R.pathOr([], ['data', 'list'], res)
        })
        props['multiSaveApi'] = 'productApi/multipleSaveProductPutInStorage'
        props['editTableConfig'] = this.editTableConfig
      }else {
        this.initOrderDetailList('orderDetailId')
        this.initWarehouseInfoList()
      }

      this.$refs.dialog.initDialog(props)
    }
  }
}
</script>
